package com.example.gpsrecorder;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.SimpleTimeZone;
import java.util.Timer;
import java.util.TimerTask;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {
	
	GPSTracker gps;
	AccelerometerTracker accelerometer;
	Logger logger;
	Calendar calendar;
	
	TextView latitudeText;
	TextView longitudeText;
	TextView altitudeText;
	TextView vitesseText;
	TextView timeText;
	TextView axText, ayText, azText;
	Button startStop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        
        latitudeText = (TextView) findViewById(R.id.LatitudeText);
    	longitudeText = (TextView) findViewById(R.id.LongitudeText);
    	altitudeText = (TextView) findViewById(R.id.AltitudeText);
    	vitesseText = (TextView) findViewById(R.id.VitesseText);
    	timeText = (TextView) findViewById(R.id.TimeText);
    	axText = (TextView) findViewById(R.id.axText);
    	ayText = (TextView) findViewById(R.id.ayText);
    	azText = (TextView) findViewById(R.id.azText);
    	startStop= (Button) findViewById(R.id.button1);
    	
    	gps = new GPSTracker(MainActivity.this);
        if(!gps.canGetLocation())
            gps.showSettingsAlert();
        
        accelerometer = new AccelerometerTracker(MainActivity.this);
        logger = new Logger(MainActivity.this);
        
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new updateTask(), 0, 1000);
        
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    class updateTask extends TimerTask {
    	
         @Override
         public void run() {
        	 runOnUiThread(new Runnable() {
        		 @Override
                 public void run() {
        			 Calendar c = Calendar.getInstance();
        			 
        			 PositionInformation positionInformation = new PositionInformation();
        			 positionInformation.time = c.getTime();
        			 positionInformation.latitude = gps.getLatitude();
        			 positionInformation.longitude= gps.getLongitude();
        			 positionInformation.altitude = gps.getAltitude();
        			 positionInformation.speed = gps.getSpeed();
        			 positionInformation.ax = accelerometer.getAx();
        			 positionInformation.ay = accelerometer.getAy();
        			 positionInformation.az = accelerometer.getAz();
        			 
        			 latitudeText.setText(Double.toString(positionInformation.latitude));
        			 longitudeText.setText(Double.toString(positionInformation.longitude));
        			 altitudeText.setText(Double.toString(positionInformation.altitude));
        			 vitesseText.setText(Double.toString(positionInformation.speed));
        			 
        			 SimpleDateFormat s = new SimpleDateFormat("H:m:s");
        			 timeText.setText(s.format(positionInformation.time));
        			 
        			 axText.setText(Float.toString(positionInformation.ax));
        			 ayText.setText(Float.toString(positionInformation.ay));
        			 azText.setText(Float.toString(positionInformation.az));
        			 
        			 if(logger.canLog()){
        				 logger.log(positionInformation);
        			 }
        			 
        		 }
        	 });
         }
    };
    
    public void hitStart(View v){
    	if(logger.canLog()) {
    		logger.stopLogging();
    		startStop.setText("Start");
    	}
    	else {
    		logger.startLogging();
    		startStop.setText("Stop");
    	}
    }
    
    public void hitExit(View v){
    	logger.stopLogging();
    	gps.stopUsingGPS();
    	accelerometer.StopUsingAcclerometer();
    	finish();
    	System.exit(0);
    }
}
